<template>
  <t-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok');
      }
    "
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item
          :label="$ct('parentOrg','父级机构')"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="type === '2'"
          required
        >
          <tree-select
            v-decorator="['pid', { initialValue: pid }]"
            :treeValueId="pid.toString()"
            placeholder="请选择"
            :parameter="parameter"
            :url="url"
            :data="treeData"
            idfield="id"
            textfield="text"
            @change="seleteOrg"
          />
        </a-form-item>
        <template v-else>
          <a-form-item class="hiddenItem">
            <a-input
              v-decorator="['pid', { initialValue: 1 }]"
              disabled
              type="hidden"
            />
          </a-form-item>
        </template>
        <a-form-item
          :label="$ct('classificationName','分类名称')"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="[
              'text',
              { rules: [{ required: true, min: 1, message: '必填！' }] },
            ]"
          />
        </a-form-item>
        <a-form-item :label="$ct('sort','排序')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number
            id="inputNumber"
            style="width: 100%;"
            :min="1"
            :precision="0"
            v-decorator="[
              'sort',
              { rules: [{ required: true, message: '必填！' }] },
            ]"
          />
        </a-form-item>
        <a-form-item
          :label="$ct('organizationNodes','是否有子节点')"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            name="radioGroup"
            v-decorator="[
              'state',
              { initialValue: 'closed' },
              { rules: [{ required: true, min: 1, message: '必填！' }] },
            ]"
          >
            <a-radio value="closed">
              是
            </a-radio>
            <a-radio value="open">
              否
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :label="$ct('describe','描述')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="4" v-decorator="['description']" />
        </a-form-item>
        <a-form-item class="hiddenItem">
          <a-input
            v-decorator="['levelId', { initialValue: levelId }]"
            disabled
            type="hidden"
          />
        </a-form-item>
        <a-form-item class="hiddenItem">
          <a-input v-decorator="['uuid']" disabled type="hidden" />
        </a-form-item>
      </a-form>
    </a-spin>
  </t-modal>
</template>

<script>
import pick from "lodash.pick";
// 表单字段
const fields = [
  "uuid",
  "description",
  "id",
  "pid",
  "levelId",
  "state",
  "text",
  "sort",
];
import {PersonnelPopup} from "@/components";
import TreeSelect from "@/components/TopVUI/custom/TreeSelect";
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
    type: {
      type: String,
    },
  },
  components: {
    TreeSelect,
		PersonnelPopup
  },
  data() {
    return {
			title: "",
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 7,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 13,
        },
      },
      form: this.$form.createForm(this),
      treeData: [],
      levelId: 1,
      pid: 1,
      showFlag: true,
      url: {
        fathersUrl: "/system/sysDataAuthGroup/getListByPid",
        expandUrl: "/system/sysDataAuthGroup/getListByPid",
      },
      parameter: {
        father: {
          pid: 1,
        },
        children: {},
      },
    };
  },
  methods: {
    add(m, data) {
      this.treeData = data ? data : [];
      this.pid = m ? m.id : "";
    },
    edit(m, data) {
      this.form.setFieldsValue(pick(m, fields));
      this.treeData = data;
      this.pid = m.pid;
    },
    seleteOrg(value) {
			this.pid = value;
    },
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v));

    // 当 model 发生改变时，为表单设置值
    this.$watch("model", () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields));
    });
  },
};
</script>
